import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

// 学生信息管理类
public class studentManager {
    private String fileName;

    public studentManager(String fileName) {
        this.fileName = fileName;
    }

    // 保存学生信息到文件
    public void saveStudents(List<Student> students) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName))) {
            oos.writeObject(students);
            System.out.println("学生信息已保存到文件。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 从文件读取学生信息
    public List<Student> loadStudents() {
        List<Student> students = new ArrayList<>();
        File file = new File(fileName);
        if (file.exists()) {
            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName))) {
                students = (List<Student>) ois.readObject();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return students;
    }

    // 根据学号查找学生
    public Student findStudentById(String studentId) {
        List<Student> students = loadStudents();
        for (Student student : students) {
            if (student.getStudentId().equals(studentId)) {
                return student;
            }
        }
        return null;
    }

    // 主方法，程序入口
    public static void main(String[] args) {
        studentManager manager = new studentManager("students.dat");
        Scanner scanner = new Scanner(System.in);

        // 输入学生信息
        List<Student> students = new ArrayList<>();
        System.out.println("请输入学生信息（格式：学号 姓名 年龄 平均成绩，输入over结束）：");
        while (true) {
            String input = scanner.nextLine();
            if ("over".equalsIgnoreCase(input)) {
                break;
            }
            String[] parts = input.split(" ");
            if (parts.length == 4) {
                try {
                    String id = parts[0];
                    String name = parts[1];
                    int age = Integer.parseInt(parts[2]);
                    double score = Double.parseDouble(parts[3]);
                    students.add(new Student(id, name, age, score));
                } catch (NumberFormatException e) {
                    System.out.println("输入格式错误，请重新输入！");
                }
            } else {
                System.out.println("输入格式错误，请重新输入！");
            }
        }

        // 保存学生信息
        manager.saveStudents(students);

        // 根据学号查询学生信息
        System.out.println("\n请输入要查询的学生学号（输入over结束）：");
        while (true) {
            String studentId = scanner.nextLine();
            if ("over".equalsIgnoreCase(studentId)) {
                break;
            }
            Student student = manager.findStudentById(studentId);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生信息！");
            }
        }

        scanner.close();
    }
}    